<template>
  <div v-loading="loading" element-loading-text="模型生成中...">
    <div style="width: 100%; border: 1px solid white;">
      <el-form  ref="form" label-width="90px">
        <el-form-item label="梢根比">
          <el-input size="small" v-model.number="taperRatio" type="number" placeholder="请输入内容">
          </el-input>
        </el-form-item>
        <el-form-item label="展弦比">
          <el-input size="small" v-model.number="aspectRatio" type="number" placeholder="请输入内容">
          </el-input>
        </el-form-item>
        <el-form-item label="前缘后掠角">
          <el-input size="small" v-model.number="LeadingEdgeSweepAngle" type="number" placeholder="请输入内容">
            <i slot="suffix" style="font-style:normal; color: #606266;margin-right: 5px">°</i>
          </el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: center; margin-top: 10px">
        <el-button type="primary" size="small" @click="creatShape">确定</el-button>
        <el-button size="small" @click="closeDialog">取消</el-button>
      </div>

    </div>
  </div>
</template>

<script>
  import {
    Axis,
    CrownCADPlugin,
    Direction, PluginCADToolFunctionType,
    PluginFunctionType,
    Point
  } from "../../../../nodeApp/static/lib/crowncad-plugin-sdk-0.0.1";
export default {
  name: "AerodynamicShape",
  data() {
    return {
      loading: false,
      crownCADPlugin: null,
      minHeight: 150,
      curvedSurface: null,
      radio: "2",
      LeadingEdgeSweepAngle: 30,
      taperRatio:0.25,
      aspectRatio: 8,
      start: 0,
      end: 0,
      start1: 0,
      end1: 0,
      fl: 0,
      sl: 0,
      b: 0,
      newPlaneId:0,
      curveId: 0,
      newCurveId: 0,
      line: null,

    }
  },
  async mounted() {
    //创建插件对象
    this.crownCADPlugin = new CrownCADPlugin();
    //设置CrownCAD平台地址
    this.crownCADPlugin.setParentOrigin(window.url);
    //.设置与CrownCAD平台尝试建立连接的次数
    this.crownCADPlugin.setMaxAttempt(5);
    //设置插件在CrownCAD平台显示的尺寸
    this.crownCADPlugin.setLayout(350, 350, this.minHeight);
    // 发送与CrownCAD平台建立连接的请求
    await this.crownCADPlugin.connect()
    //设置对话框的大小
    await this.crownCADPlugin.request.execFunction(PluginFunctionType.LAYOUT, { minWidth: 350, width: 350, maxHeight: this.minHeight})

    //获取翼型草图中的翼型曲线的id，以及两端点的坐标
    await this.getAll()

  },
  methods: {
    //关闭对话框
    closeDialog() {
      this.crownCADPlugin.request.execFunction(PluginCADToolFunctionType.CLOSEACTIVECMD, null);
    },
    // 创建按钮事件
    async creatShape() {
      // 初始化数据
      this.initData()
      // 创建距翼型距离的基准面，再次基准面绘制气动外形的另一端
      this.newPlaneId= await this.createBasicPlane(8, this.b* 0.5, "基准面1")
      // 在新的基准面上绘制另一端翼型草图
      await this.createShapeSketch()
      // 获取草图sketch1中的元素
      let res1 = await this.crownCADPlugin.query.getElementsByFeatureNames(["sketch1"]);

      res1.data.elements.forEach(element => {
        if(element.type=== 8) {
          // 找到element中type的元素，即为新的翼型草图（此草图中只有一个有用的元素，故只判断element是的type即可）
          this.newCurveId= element.id
          // sketch= entity.featureId
        }
      })
      // 修改草图sketch1，将翼型草图根据梢根比进行缩放
      await this.editShapeSketch()

      // 调用放样曲面方法，生成气动外形
      await this.loftSurface()
    },

    initData() {
      // 计算前缘后掠角从角度转换为弧度
      let lesa = (this.LeadingEdgeSweepAngle / 180) * Math.PI;
      // 计算翼型的宽度
      this.fl= this.end- this.start
      // 根据梢根比计算气动外形另一端的宽度
      this.sl= this.fl* this.taperRatio
      // 计算气动外形的长度，b为外形的总长度，一半即为一端外形的长度
      this.b= this.aspectRatio* (this.fl+ this.sl)*0.5
      // 计算气动外形另一端的起点
      this.start1= this.start+ this.b* 0.5* Math.tan(lesa)
      // 计算气动外形另一端的终点
      this.end1= this.start1+ this.sl

    },

    async createBasicPlane(planeId, len, name) {
      //SDK方法，根据距离创建基准面
      let params = {
          planeId:planeId,
          invertNormal: 1,
          paralDis:len
      };
      let ret = await this.crownCADPlugin.command.datum.createPlaneByDistanceIncrement(params, name);
      //  获取创建的基准面id
      return await this.getBasciPlaneId(name)
    },
    async getBasciPlaneId(name) {
      // 根据基准面的名字获取基准面的id，为SDK中方法
      this.crownCADPlugin.command.clearCommand();
      // 获取草图1的featuresid
      let res= await this.crownCADPlugin.query.getFeaturesByNames([name])
      // 获取草图1的entityid
      let res1= await this.crownCADPlugin.query.getEntitiesByFeatureIds([res.data.features[0]?.id])
      // 返回草图1的entityid
      return res1.data.entities[0].id
    },

    async createShapeSketch() {
      this.crownCADPlugin.command.clearCommand();
      //创建草图，命名为sketch1
      this.crownCADPlugin.command.sketch.createSketch({sketchName: 'sketch1', datumId: this.newPlaneId});

      //使用草图转换边界方法，将翼型绘制到新基准面上
      this.crownCADPlugin.command.sketch.convertEdge({
        edgeIds: [this.curveId], })
      //退出草图
      this.crownCADPlugin.command.sketch.exitSketch();
      //实现草图操作
      await this.crownCADPlugin.command.execute()
    },

    async editShapeSketch() {
      this.crownCADPlugin.command.clearCommand();
      // 编辑草图sketch1
      this.crownCADPlugin.command.sketch.editSketch({sketchName: 'sketch1'});
      // 缩放草图sketch1
      this.crownCADPlugin.command.sketch.scaleElements({
            elementIds: [this.newCurveId],
            scalePnt: new Point(0,0,0),
            scaleRef: this.taperRatio,
            copyFlag: 0,
            copyNum: 0
      })
      // 将缩放的翼型移到新的起点和终点
      this.crownCADPlugin.command.sketch.moveElements({
        elementIds: [this.newCurveId],
        startPnt: new Point(0,0,0),
        endPnt: new Point((this.end1+ this.start1)*0.5,0,0),
      })
      //退出编辑模式
      this.crownCADPlugin.command.sketch.exitEditSketch({sketchName: 'sketch1'});
      await this.crownCADPlugin.command.execute()
    },

    async loftSurface() {
      this.crownCADPlugin.command.clearCommand();
      // 新翼型和初始翼型进行放样操作生成气动外形
      this.crownCADPlugin.command.surface.loftSurface({
        profiles:[this.curveId, this.newCurveId],
        hasGuideCurve:0,
        guideCurves:[],
      })
      await this.crownCADPlugin.command.execute()
    },

    async getAll() {
      this.crownCADPlugin.command.clearCommand()
      let res= await this.crownCADPlugin.query.getEntities()
      console.log(res);
      res.data.entities.forEach(entity=> {
        if(entity.type=== 8) {
          // 找到element中type的元素，即为翼型草图（此草图中只有一个有用的元素，故只判断element是的type即可）
          this.curveId= entity.id
          // sketch= entity.featureId
        }
      })
      // 根据名称获取元素
      let res1 = await this.crownCADPlugin.query.getElementsByFeatureNames(["草图1"]);

      res1.data.elements.forEach(element => {
        // 筛选element中的type为10的直线，获取起始点，为翼型的两端点
        if(element.type=== 8 && element.params.type===10) {
          this.start= element.params.startPoint[0]
          this.end= element.params.endPoint[0]

          // sketch= entity.featureId
        }
        // 筛选element中的type为15的曲线，即翼型曲线
        // if(element.type=== 8 && element.params.type===15) {
        //   this.curveId= element.id
        // }
      })
    }

  }
}
</script>

<style scoped>
>>> .el-form-item {
  margin-bottom: 5px!important;
}
>>> .el-form-item__label {
  margin-bottom: 5px!important;
}

>>> .el-form-item__content {
  display: flex;
  align-items: center;
}

>>> .el-form-item {
  height: 32px!important;
}
>>> .el-form-item__label {
  line-height: 32px !important;
}
.selectItem {
  background-color: #d6eaff;
  color: #212529;
  border: 1px solid #6e7177;
  border-radius: .2rem;
  margin: .5rem 0rem 0.25rem .5rem;
  padding: .25rem .25rem .25rem .5rem;
  font-size: 12px;
  position: relative;
  cursor: pointer;
}
.color-bar {
  border-radius: 0 0 10px 0;
  height: 10px;
  left: 0;
  position: absolute;
  top: 0;
  -webkit-transition: .2s;
  transition: .2s;
  width: 10px;
}
.selectTitle {
  height: 24px;
  line-height: 24px;
}
.selectedItem {
  margin-left: 20px;
  padding: 5px 15px 5px 5px;
  border-radius: 5px;
  display: flex;
  justify-content: space-between;
}
.selectedItem:hover {
  background-color: rgba(0,0,0,.1)/* 鼠标悬浮时的背景颜色 */
}

</style>
